package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;
    /**
     * 支付超时更新单据状态
     */
    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutTimeLT(){
        System.out.println("查询超时支付单据");
        log.info("查询超时支付单据...");
        LocalDateTime preTime = LocalDateTime.now().plusMinutes(-15);
        //待付款且超时十五分钟
        List<Orders> orders = orderMapper.getByStatusAndTimeOutLT(Orders.PENDING_PAYMENT,preTime);

        if (orders!=null || orders.size() > 0){
            log.info("处理超时支付单据中...");
            for (Orders o:orders) {
                o.setStatus(Orders.CANCELLED);
                o.setCancelReason( "订单支付超时,自动取消");
                o.setCancelTime(LocalDateTime.now());
                orderMapper.update(o);
            }
        }
    }

    /**
     * 订单配送超时自动完成
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void deliveryTimeOut(){
        System.out.println("查询超时支付单据");
        log.info("查询超时支付单据...");
        LocalDateTime preTime = LocalDateTime.now().plusDays(-1);
        //待收货超时
        List<Orders> orders = orderMapper.getByStatusAndTimeOutLT(Orders.DELIVERY_IN_PROGRESS,preTime);

        if (orders!=null || orders.size() > 0){
            log.info("处理超时支付单据中...");
            for (Orders o:orders) {
                o.setStatus(Orders.CANCELLED);
                o.setCancelReason( "订单完成超时，自动签收");
                o.setCancelTime(LocalDateTime.now());
                orderMapper.update(o);
            }
        }
    }


}
